package com.jsmile.cloud.tradecenter.order.repo;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderRefund;
import com.jsmile.cloud.tradecenter.order.dao.OrderProductDao;
import com.jsmile.cloud.tradecenter.order.model.OrderProduct;
import com.jsmile.mall.api.enums.JSmileCacheEnum;

import lombok.AllArgsConstructor;

@Component
@AllArgsConstructor
@Transactional(readOnly = true)
public class OrderProductSearchRepository extends ServiceImpl<OrderProductDao, OrderProduct> {

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'products_'+#orderNo")
    public List<OrderProduct> findByOrderNo(Long orderNo) {
        return this.baseMapper.selectList(new QueryWrapper<OrderProduct>().lambda().eq(OrderProduct::getOrderNo, orderNo));
    }

    public List<OrderProduct> findByRefundOrder(ReqOrderRefund reqOrderRefund) {
        if (reqOrderRefund.getHasAllRefund()) {
            return this.findByOrderNo(reqOrderRefund.getOrderNo());
        }
        return this.baseMapper
            .selectList(new QueryWrapper<OrderProduct>().lambda().in(!CollectionUtils.isEmpty(reqOrderRefund.getSkuIds()), OrderProduct::getSkuId, reqOrderRefund.getSkuIds())
                .eq(OrderProduct::getOrderNo, reqOrderRefund.getOrderNo()));
    }
}
